

import statistics
from enum import Enum


class StructureType(str, Enum):
    CLEAR = "清晰结构"
    SINGLE = "单节点"
    UNCLEAR = "不清晰结构"
def get_sample_structure_type(row: Sample) -> StructureType:
    sentences = row['sentences']
    is_title_flags = row['is_title_flags']
    labels = row['labels']
    BOS_sentences = [s for (s, l) in zip(sentences, labels) if l == 'BOS']
    if len(BOS_sentences) >= 4:
        sub_BOS_sentences = BOS_sentences[1:-1]
    elif len(BOS_sentences) == 3:
        sub_BOS_sentences = BOS_sentences[1:]
    elif len(BOS_sentences) == 2:
        sub_BOS_sentences = BOS_sentences
    elif len(BOS_sentences) == 1:
        return StructureType.SINGLE
    else:
        raise ValueError("No BOS.")
    assert len(sub_BOS_sentences) >= 2
    if set([s[0] for s in sub_BOS_sentences]) in [set('-'), set('('), set('（'), set('第')]:
        return StructureType.CLEAR
    if all([len(s) > 1 for s in sub_BOS_sentences]):
        if len(set([s[1] for s in sub_BOS_sentences])) == 1 and not sub_BOS_sentences[0][1].isdigit():
            return StructureType.CLEAR
    BOS_is_clear_scores = []
    for s, t, l in zip(sentences, is_title_flags, labels):
        if l == 'BOS':
            score = 0
            if s[-1] not in '.。':
                score += 1
            if t:
                score += 1
            if len(s) > 25:
                score -= 1
            BOS_is_clear_scores.append(score)
    if any([score < 0 for score in BOS_is_clear_scores[1:-1]]):
        return StructureType.UNCLEAR
    return StructureType.CLEAR if statistics.mean(BOS_is_clear_scores[1:]) >= 1 else StructureType.UNCLEAR